import { defineStore } from "pinia";
import request from '@/api/request'
import { ElMessage } from "element-plus";
import router from '@/router'
import Layout from '@/views/Layout.vue'
import Hris_master_data from '@/views/Hris_master_data.vue'
let modules = import.meta.glob("../views/**/*.vue");

//推荐使用use名称开头
//state定义数据
//getter是用来做数据处理，如果不要处理数据，就不要定义
//action是用来定义函数，一般是异步函数
let useSys = defineStore("useSys", {
    state: () => {
        let user = undefined
        let treeMenus = undefined
        let menus = undefined
        let tabs = []
        let activeTab = "home"
        let token = "123"
        return { user, menus, treeMenus, tabs, activeTab, token }
    },
    getters: {
        sessionUser() {
            if (!this.user) {
                let user = sessionStorage.getItem("user");
                this.user = JSON.parse(user);
            }
            return this.user;
        },
        sessionToken() {
            if (!this.token) {
                this.token = localStorage.getItem("token");
            }
            return this.token;
        }
    },
    actions: { //async异步函数，如果函数中有远程请求的，那么这个方法就使用异步
        async login(username, password) {
            let result = false;
            //async 定义异步函数
            //await 等待异步函数执行完毕
            await request({
                url: "submitLogin",
                method: "post",
                params: {
                    username,
                    password
                }
            }).then(resp => {
                console.log(resp)
                if (resp.code == 200) {
                    result = true;
                    localStorage.setItem("token", resp.data)
                }
            });
            return result;
        },
        async loadMenus() {
            //树形结构
            await request.get("sysMenu/tree").then(resp => {
                console.log(resp.data)
                this.treeMenus = resp.data
            });
            //没有树形结构
            await request.get("sysMenu/all").then(resp => {
                console.log(resp.data)
                this.menus = resp.data
                resp.data.forEach(item => {
                    if (item.component) {
                        //新增属性
                        item.name = item.ename;
                        if (item.component == "Layout") {
                            //修改属性：component传入来的时候是string类型，
                            item.component = Layout
                            //item是路由对象{path,name,componet}
                            router.addRoute(item)
                        }
                        else if (item.component == "Hris_master_data") {
                            //修改属性
                            item.component = Hris_master_data
                            //addRoute：一个是一个参数的方法，加载根路由
                            // addRoute(parentname,route)：第一个参数是父级的路由名称，后面是要添加的路由
                            router.addRoute(item.parentName, item)
                        }
                        else {
                            //根据路径找到指定的vue文件，让item的component变成vue组件
                            item.component = modules[`../views/${item.component}.vue`]
                            router.addRoute(item.parentName, item)
                        }
                    }
                });
            });
            return true;
        },
        handleClickMenu(item) {
            //查找数组中是否已经存在了当前单击的菜单
            let flag = this.tabs.some(e => e.ename == item.ename);
            this.activeTab = item.ename;
            if (flag) return;
            //添加选项卡
            this.tabs.push(item);
        },
        async hasPermission(perm) {
            let m = this.menus.find(item => item.perms == perm)
            if (!m) {
                return Promise.reject("没有权限")
            }
            return Promise.resolve(m)
        }
    }
});

export default useSys;